package com.situ.util.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.situ.util.service.ProductService;
import com.situ.util.entity.Product;
import com.situ.util.*;

import java.util.List;

/**
 * Product的路由接口服务
 * 
 * @author 
 *
 */
@RestController
public class ProductController {

	/** ProductService服务 */
	@Autowired
	private ProductService productService;
	
	/**
	 * 查询所有Product数据的方法
	 * @param value
	 * @return
	 */
	@RequestMapping(value = "queryProduct", produces = {"application/json;charset=UTF-8"})
	public ResponseData find(Product value) {

		return ResponseData.ok().putDataValue("list",productService.find(value));
	}
	
	/**
	 * 通过id查询Product数据的方法
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "queryProductById/{id}", produces = {"application/json;charset=UTF-8"})
	public ResponseData findOne(@PathVariable(name="id") Integer id) {
		 return ResponseData.ok().putDataValue("obj",productService.findOne(id));
	}
	
	/**
	 * 插入Product属性不为空的数据方法
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "saveProduct")
	public ResponseData save(Product value) {
		return ResponseData.ok().putDataValue("obj", productService.saveNotNull(value));
	}
	
	/**
	 * 更新Product属性不为空的数据方法
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "updateProduct", produces = {"application/json;charset=UTF-8"})
	public ResponseData update(Product value) {
		return ResponseData.ok().putDataValue("obj",productService.updateNotNullById(value));
	}

	/**
	 * 通过id删除Product数据方法
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "delProductById/{id}", produces = {"application/json;charset=UTF-8"})
	public ResponseData delete(@PathVariable(name="id") Integer id) {
		return ResponseData.ok().putDataValue("obj",productService.deleteById(id));
	}

	@RequestMapping("selectProductByLike")
	public ResponseData selectProductByLike(Product value,
											@RequestParam(defaultValue ="1", required = false) int startPage ,
											@RequestParam(defaultValue = "5",required = false) int PageSize){

		PageHelper.startPage(startPage,PageSize );
		List<Product> list = productService.selectProductByLike(value);
		PageInfo pageInfo=new PageInfo(list,5);
		return ResponseData.ok().putDataValue("pageInfo",pageInfo);
	}
}
